package org.tulkas.base;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.Test;
import org.tulkas.base.FlusherManager.FlusherChecker;
import org.tulkas.base.FlusherManager.FlusherItem;
import org.tulkas.test.TulkasCase;
import org.tulkas.util.IFlushable;
import org.tulkas.util.LangUtil;

/**
 * @author <a href="mailto:tulkas.web@gmail.com">Tulkas</a>
 *
 */
public class FlusherManagerTest extends TulkasCase {
	private int flushCount = 0;
	private boolean isModified = false;

	@SuppressWarnings("unchecked")
	@Test
	public void testFlusherManager() throws Exception {
		FlusherManager fm = FlusherManager.getInstance();
		FlusherManager.FlusherChecker checker = (FlusherChecker) LangUtil
				.getAnyField(fm, "checker");
		assertEquals(true, checker.isRunning());

		IFlushable flusher = new IFlushable() {
			public boolean needFlush() {
				return isModified;
			}

			public void flush() {
				flushCount++;
			}
		};

		fm.addFlusher(flusher, 1);
		Thread.sleep(1200);
		assertEquals(0, flushCount);
		List<FlusherItem> list = (List<FlusherItem>) LangUtil.getAnyField(fm,
				"flushers");
		assertEquals(1, list.size());
		isModified = true;
		Thread.sleep(1200);
		assertEquals(1, flushCount);
		FlusherManager.getInstance().flushAll();
		assertEquals(2, flushCount);
		FlusherManager.getInstance().removeFlusher(flusher);
		assertEquals(0, list.size());
	}
}
